<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/19
 * Time: 17:13
 */
if (!function_exists('createUniqueID')) {
    /**
     * 生成唯一标识符
     * @return false|string
     */
    function createUniqueID()
    {
        $chars  =   md5(uniqid(mt_rand(), true));
        $uuid   =   substr ( $chars, 0, 8 ) . '-'
                    . substr ( $chars, 8, 4 ) . '-'
                    . substr ( $chars, 12, 4 ) . '-'
                    . substr ( $chars, 16, 4 ) . '-'
                    . substr ( $chars, 20, 12 );

        return substr(strtoupper($uuid), 0, 36);
    }
}

if (!function_exists('createRandomNumber')) {
    /**
     * 生成指定长度的随机数字
     * @param int $length
     * @return string
     */
    function createRandomNumber (int $length = 10)
    {
        $string =   '';
        for ($i = 0; $i < $length; $i++) {
            $string .=  rand(0, 9);
        }

        return $string;
    }
}

if (!function_exists('xmlEncode')) {
    /**
     * XML编码
     * @param mixed $data 数据
     * @param string $root 根节点名
     * @param string $encoding 数据编码
     * @return string
     */
    function xmlEncode($data, $root = 'response', $encoding = 'utf-8') {
        $xml    = '<?xml version="1.0" encoding="' . $encoding . '"?>';
        $xml   .= '<' . $root . '>';
        $xml   .= dataToXml($data);
        $xml   .= '</' . $root . '>';
        return $xml;
    }
}

if (!function_exists('dataToXml')) {
    /**
     * 数据XML编码
     * @param mixed $data 数据
     * @return string
     */
    function dataToXml($data) {
        $xml = '';
        foreach ($data as $key => $val) {
            if (is_numeric($key) && is_array($val)) {
                $xml    .=  ( is_array($val) || is_object($val)) ? dataToXml($val) : $val;
            } else {
                $xml    .=  "<$key>";
                $xml    .=  ( is_array($val) || is_object($val)) ? dataToXml($val) : $val;
                $xml    .=  "</$key>";
            }
        }
        return $xml;
    }
}

if (!function_exists('xmlObjectToArray')) {
    /**
     * xmlObjectToArray  xml对象转array，解决xml空元素的情况下转成空数组的问题
     * @param $object
     * @return mixed
     */
    function xmlObjectToArray($object)
    {
        $result = [];
        if (is_object($object)) {
            $object = get_object_vars($object);
        }

        if (is_array($object)) {
            foreach ($object as $key => $vo) {
                if (is_object($vo)) {
                    dump($vo);
                    $vo =   xmlObjectToArray($vo);
                    dump($vo);
                }

                if (is_array($vo)) {
                    $vo =   arrayObjectToArray($vo);
                }

                if ($key != '@attributes') {
                    $result[$key] = $vo ? : '';
                }

            }
        }
        return $result;
    }
}

if (!function_exists('arrayObjectToArray')) {
    /**
     * @param $array
     * @return mixed
     */
    function arrayObjectToArray($array = [])
    {
        $result = [];
        if (!empty($array)) {
            foreach ($array as $key => $value) {
                if (is_object($value)) {
                    $value  =   xmlObjectToArray($value);
                }
                if (is_array($value)) {
                    $value  =   arrayObjectToArray($value);
                }
                $result[$key]   =   $value;
            }
        }
        return $result;
    }
}

if (!function_exists('wechatEncrypt')) {
    /**
     * 微信支付加密方式
     * @param array $params
     * @param string $encrypt_key
     * @param string $encrypt_type
     * @return string
     */
    function wechatEncrypt(array $params, string $encrypt_key, string $encrypt_type = 'MD5')
    {
        ksort($params);
        $encrypt_string =   '';

        foreach ($params as $key => $item) {
            if ($key == 'sign') {
                continue;
            }
            $encrypt_string .=  $key . '=' . $item . '&';
        }

        $encrypt_string .=  'key=' . $encrypt_key;
        if ($encrypt_type == 'MD5') {
            return strtoupper(md5($encrypt_string));
        } else {
            return strtoupper(hash_hmac('sha256', $encrypt_string, $encrypt_key));
        }
    }
}
